package main

import (
	"encoding/hex"
	"fmt"
	"log"
	"strings"
	"tt/dtl"
)

func main() {
	m := dtl.Meter{
		MeterAddr: "000000005255",
		MeterType: "DTl07",
	}
	var d = new(dtl.DL64597)
	s, err := dtl.OpenPort("COM2")
	if err != nil {
		 log.Printf("Error opening port: %v", err)
		 return
	}
	defer s.Close()
	d.GetKvi(&m, s)

	log.Printf("KVI: %#v", m)
	// m := dtl.Meter{
	// 	MeterAddr: "000000005255",
	// }
	// var d *dtl.DL64507 = new(dtl.DL64507)
	// // sts := "FE FE FE FE 68 55 52 00 00 00 00 68 91 0B 33 32 34 35 33 59 33 55 33 55 35 B2 16"
	// // sts = strings.Replace(sts, " ", "", -1)
	// //bys, _ := hex.DecodeString("fefefefe6800514418111768910733343535333333a716")
	// // bys, _ := hex.DecodeString(sts)
	// // d.Parse(bys)
	// d.GetKvi(&m, "COM2")
	// log.Printf("KVI: %#v", m)
	// fmt.Println("----------------------------------------------------->")
	// time.Sleep(time.Second * 10)
	// m2 := dtl.Meter{
	// 	MeterAddr: "000000005255",
	// }

	// var d1 *dtl.DL64597 = new(dtl.DL64597)
	// // sts := "FE FE FE FE 68 55 52 00 00 00 00 68 91 0B 33 32 34 35 33 59 33 55 33 55 35 B2 16"
	// // sts = strings.Replace(sts, " ", "", -1)
	// //bys, _ := hex.DecodeString("fefefefe6800514418111768910733343535333333a716")
	// // bys, _ := hex.DecodeString(sts)
	// // d.Parse(bys)
	// d1.GetKvi(&m2, "COM2")
	// log.Printf("%+v", m2)
	// log.Printf("%+v", m)
}

func main1() {
	var Dtl07 dtl.DL64507
	sts := "FE FE FE FE 68 55 52 00 00 00 00 68 91 07 33 33 39 35 B5 39 35 06 16"
	sts = "FE FE FE FE 68 55 52 00 00 00 00 68 91 0B 33 32 34 35 33 59 33 55 33 55 35 B2 16 "
	sts = "FE FE FE FE 68 55 52 00 00 00 00 68 91 0E 33 32 35 35 33 33 43 33 33 43 33 33 3A 35 0C 16 "
	sts = "FE FE FE FE 68 55 52 00 00 00 00 68 91 09 34 34 33 37 36 37 45 57 35 21 16 "
	sts = "FE FE FE FE 68 55 52 00 00 00 00 68 91 08 35 34 33 37 85 43 47 35 27 16 "
	sts = "FE FE FE FE 68 55 52 00 00 00 00 68 91 19 33 32 34 33 BC A5 34 33 33 33 33 33 3A 87 33 33 93 38 34 33 4B 47 33 33 35 07 16 "
	sts = "FE FE FE FE 68 55 52 00 00 00 00 68 91 0B 33 32 34 35 33 59 33 55 33 55 35 B2 16"
	sts = strings.Replace(sts, " ", "", -1)
	//bys, _ := hex.DecodeString("fefefefe6800514418111768910733343535333333a716")
	bys, _ := hex.DecodeString(sts)
	Dtl07.Parse(bys)
	zl, da, bl := Dtl07.UnpakeData()
	fmt.Println(zl, da, bl)
	ft := Dtl07.GetValue(da, 4, 10)
	fmt.Println("获取状态", ft)
	fmt.Println("------------------")
	//电流读取 abc
	sts = "FE FE FE FE 68 55 52 00 00 00 00 68 91 0E 33 32 35 35 33 33 43 33 33 43 33 33 3A 35 0C 16"
	sts = strings.Replace(sts, " ", "", -1)
	//bys, _ := hex.DecodeString("fefefefe6800514418111768910733343535333333a716")
	bys, _ = hex.DecodeString(sts)
	Dtl07.Parse(bys)
	zl, da, bl = Dtl07.UnpakeData()
	fmt.Println(zl, da, bl)
	ft = Dtl07.GetValue(da, 6, 1000)
	fmt.Println("获取电流", ft)
	fmt.Println("------------------")
	//总功率
	sts = "FE FE FE FE 68 55 52 00 00 00 00 68 91 19 33 32 34 33 44 35 35 33 33 33 33 33 3A 87 33 33 93 38 34 33 73 76 33 33 35 77 16 "
	sts = "FE FE FE FE 68 55 52 00 00 00 00 68 91 09 33 33 34 33 BC 35 35 33 35 6C 16 "
	sts = strings.Replace(sts, " ", "", -1)
	//bys, _ := hex.DecodeString("fefefefe6800514418111768910733343535333333a716")
	bys, _ = hex.DecodeString(sts)
	Dtl07.Parse(bys)
	zl, da, bl = Dtl07.UnpakeData()
	fmt.Println(zl, da, bl)
	ft = Dtl07.GetValue(da, 8, 100)
	fmt.Println("获取电流", ft)
	fmt.Println("------------------")
	// 电压获取
	sts = "FE FE FE FE 68 55 52 00 00 00 00 68 91 0B 33 32 34 35 33 59 33 49 33 55 35 A6 16 "
	sts = strings.Replace(sts, " ", "", -1)
	//bys, _ := hex.DecodeString("fefefefe6800514418111768910733343535333333a716")
	bys, _ = hex.DecodeString(sts)
	Dtl07.Parse(bys)
	zl, da, bl = Dtl07.UnpakeData()
	fmt.Println(zl, da, bl)
	ft = Dtl07.GetValue(da, 4, 10)
	fmt.Println("获取电压", ft)
	fmt.Println("------------------")
	return
	fmt.Println(Dtl07)
	fmt.Println(Dtl07.AddrString())
	fmt.Println(Dtl07.ToBytes())
	fmt.Println("获取状态")
	bys, ok, err := Dtl07.GetCommand("000000005255", "00000000", 0x11)
	fmt.Printf("ok:%v,err:%v,bys:%v\n", ok, err, hex.EncodeToString(bys))
	bys, ok, err = Dtl07.GetCommand("000000005255", "00000100", 0x11)
	fmt.Printf("ok:%v,err:%v,bys:%v\n", ok, err, hex.EncodeToString(bys))
	fmt.Println("获取电压")
	bys, ok, err = Dtl07.GetCommand("000000005255", "02010100", 0x11)
	fmt.Printf("ok:%v,err:%v,bys:%v\n", ok, err, hex.EncodeToString(bys))
	bys, ok, err = Dtl07.GetCommand("000000005255", "02010200", 0x11)
	fmt.Printf("ok:%v,err:%v,bys:%v\n", ok, err, hex.EncodeToString(bys))
	bys, ok, err = Dtl07.GetCommand("000000005255", "02010300", 0x11)
	fmt.Printf("ok:%v,err:%v,bys:%v\n", ok, err, hex.EncodeToString(bys))
	bys, ok, err = Dtl07.GetCommand("000000005255", "0201FF00", 0x11)
	fmt.Printf("ok:%v,err:%v,bys:%v\n", ok, err, hex.EncodeToString(bys))
	fmt.Println("获取电流")
	bys, ok, err = Dtl07.GetCommand("000000005255", "02020100", 0x11)
	fmt.Printf("ok:%v,err:%v,bys:%v\n", ok, err, hex.EncodeToString(bys))
	bys, ok, err = Dtl07.GetCommand("000000005255", "02020200", 0x11)
	fmt.Printf("ok:%v,err:%v,bys:%v\n", ok, err, hex.EncodeToString(bys))
	bys, ok, err = Dtl07.GetCommand("000000005255", "02020300", 0x11)
	fmt.Printf("ok:%v,err:%v,bys:%v\n", ok, err, hex.EncodeToString(bys))
	bys, ok, err = Dtl07.GetCommand("000000005255", "0202ff00", 0x11)
	fmt.Printf("ok:%v,err:%v,bys:%v\n", ok, err, hex.EncodeToString(bys))
	fmt.Println("获取功率")
	bys, ok, err = Dtl07.GetCommand("000000005255", "02030000", 0x11)
	fmt.Printf("ok:%v,err:%v,bys:%v\n", ok, err, hex.EncodeToString(bys))
	fmt.Println("功率因数")
	bys, ok, err = Dtl07.GetCommand("000000005255", "02060000", 0x11)
	fmt.Printf("ok:%v,err:%v,bys:%v\n", ok, err, hex.EncodeToString(bys))
	//FE FE FE FE 68 55 52 00 00 00 00 68 91 07 33 33 39 35 B5 39 35 06 16
	bys = []byte{0xfe, 0xfe, 0xfe, 0xfe, 0x68, 0x55, 0x52, 0x00, 0x68, 0x91, 0x07, 0x33, 0x33, 0x39, 0x35, 0xb5, 0x39, 0x35, 0x06, 0x16}
}
